﻿using System;

namespace ORS.OrderRobot.BackgroundWorker
{
    /// <summary>
    /// 用于唤醒其他自执行worker的基类
    /// </summary>
    [Obsolete]
    public abstract class WakeupTaskWorkerBase : IBackgroundWorker, ICronWorker
    {
        public abstract string Name { get; }
        /// <summary>
        /// 唤醒时间
        /// </summary>
        protected abstract Task<TimeOnly> GetBeigneTime();
        /// <summary>
        /// 唤醒目标实例，由子类提供
        /// </summary>
        protected abstract IBackgroundRepeatTasker TargetInstance { get; }
        /// <summary>
        /// 执行唤醒动作
        /// </summary>
        /// <returns></returns>
        public Task ExecuteAsync()
        {
            TargetInstance.StartAsync();
            return Task.CompletedTask;
        }
        /// <summary>
        /// 生成唤醒时间的corn
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetCronExpressionAsync()
        {
            var beginTime = await GetBeigneTime();
            var m = beginTime.Minute;
            var h = beginTime.Hour;
            var s = beginTime.Second;
            
            // 标准 cron 格式：秒 分 时 日 月 星期
            return $"{s} {m} {h} * * *";
        }
        public Task StartAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
        public Task StopAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
    }
}

